'use strict';
var _typeof = 'function' == typeof Symbol && 'symbol' == typeof Symbol.iterator ? function (a) {
  return typeof a
} : function (a) {
  return a && 'function' == typeof Symbol && a.constructor === Symbol && a !== Symbol.prototype ? 'symbol' : typeof a
};
(function () {
  var a = Math,
    b = function (D) {
      return D >> 0
    },
    c = /webkit/i.test(navigator.appVersion) ? 'webkit' : /firefox/i.test(navigator.userAgent) ? 'Moz' : 'opera' in window ? 'O' : '',
    d = /android/gi.test(navigator.appVersion),
    f = /iphone|ipad/gi.test(navigator.appVersion),
    g = /playbook/gi.test(navigator.appVersion),
    h = /hp-tablet/gi.test(navigator.appVersion),
    j = 'WebKitCSSMatrix' in window && 'm11' in new WebKitCSSMatrix,
    k = 'ontouchstart' in window && !h,
    n = c + 'Transform' in document.documentElement.style,
    p = function () {
      return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (D) {
        return setTimeout(D, 17)
      }
    }(),
    q = function () {
      return window.cancelRequestAnimationFrame || window.webkitCancelAnimationFrame || window.webkitCancelRequestAnimationFrame || window.mozCancelRequestAnimationFrame || window.oCancelRequestAnimationFrame || window.msCancelRequestAnimationFrame || clearTimeout
    }(),
    s = 'onorientationchange' in window ? 'orientationchange' : 'resize',
    u = k ? 'touchstart' : 'mousedown',
    v = k ? 'touchmove' : 'mousemove',
    w = k ? 'touchend' : 'mouseup',
    z = k ? 'touchcancel' : 'mouseup',
    A = 'translate' + (j ? '3d(' : '('),
    B = j ? ',0)' : ')',
    C = function (D, E) {
      var H, F = this,
        G = document;
      for (H in F.wrapper = 'object' == ('undefined' == typeof D ? 'undefined' : _typeof(D)) ? D : G.getElementById(D), F.wrapper.style.overflow = 'hidden', F.scroller = F.wrapper.children[0], F.options = {
          hScroll: !0,
          vScroll: !0,
          x: 0,
          y: 0,
          bounce: !0,
          bounceLock: !1,
          momentum: !0,
          lockDirection: !0,
          useTransform: !0,
          useTransition: !1,
          onRefresh: null,
          onBeforeScrollStart: function onBeforeScrollStart(I) {
            I.preventDefault()
          },
          onScrollStart: null,
          onBeforeScrollMove: null,
          onScrollMove: null,
          onBeforeScrollEnd: null,
          onScrollEnd: null,
          onTouchEnd: null,
          onDestroy: null
        }, E) F.options[H] = E[H];
      F.x = F.options.x, F.y = F.options.y, F.options.useTransform = !!n && F.options.useTransform, F.options.hScrollbar = F.options.hScroll && F.options.hScrollbar, F.options.vScrollbar = F.options.vScroll && F.options.vScrollbar, F.options.useTransition = (f || g) && F.options.useTransition, F.scroller.style[c + 'TransitionProperty'] = F.options.useTransform ? '-' + c.toLowerCase() + '-transform' : 'top left', F.scroller.style[c + 'TransitionDuration'] = '0', F.scroller.style[c + 'TransformOrigin'] = '0 0', F.options.useTransition && (F.scroller.style[c + 'TransitionTimingFunction'] = 'cubic-bezier(0.33,0.66,0.66,1)'), F.options.useTransform ? F.scroller.style[c + 'Transform'] = A + F.x + 'px,' + F.y + 'px' + B : F.scroller.style.cssText += ';position:absolute;top:' + F.y + 'px;left:' + F.x + 'px', F.refresh(), F._bind(s, window), F._bind(u), k || F._bind('mouseout', F.wrapper)
    };
  C.prototype = {
    enabled: !0,
    x: 0,
    y: 0,
    steps: [],
    scale: 1,
    handleEvent: function handleEvent(D) {
      var E = this;
      switch (D.type) {
        case u:
          if (!k && 0 !== D.button) return;
          E._start(D);
          break;
        case v:
          E._move(D);
          break;
        case w:
        case z:
          E._end(D);
          break;
        case s:
          E._resize();
          break;
        case 'mouseout':
          E._mouseout(D);
          break;
        case 'webkitTransitionEnd':
          E._transitionEnd(D);
      }
    },
    _resize: function _resize() {
      this.refresh()
    },
    _pos: function _pos(D, E) {
      D = this.hScroll ? D : 0, E = this.vScroll ? E : 0, this.options.useTransform ? this.scroller.style[c + 'Transform'] = A + D + 'px,' + E + 'px' + B + ' scale(' + this.scale + ')' : (D = b(D), E = b(E), this.scroller.style.left = D + 'px', this.scroller.style.top = E + 'px'), this.x = D, this.y = E
    },
    _start: function _start(D) {
      var G, H, I, E = this,
        F = k ? D.touches[0] : D;
      E.enabled && (E.options.onBeforeScrollStart && E.options.onBeforeScrollStart.call(E, D), E.options.useTransition && E._transitionTime(0), E.moved = !1, E.animating = !1, E.zoomed = !1, E.distX = 0, E.distY = 0, E.absDistX = 0, E.absDistY = 0, E.dirX = 0, E.dirY = 0, E.options.momentum && (E.options.useTransform ? (G = getComputedStyle(E.scroller, null)[c + 'Transform'].replace(/[^0-9-.,]/g, '').split(','), H = 1 * G[4], I = 1 * G[5]) : (H = 1 * getComputedStyle(E.scroller, null).left.replace(/[^0-9-]/g, ''), I = 1 * getComputedStyle(E.scroller, null).top.replace(/[^0-9-]/g, '')), (H != E.x || I != E.y) && (E.options.useTransition ? E._unbind('webkitTransitionEnd') : q(E.aniTime), E.steps = [], E._pos(H, I))), E.startX = E.x, E.startY = E.y, E.pointX = F.pageX, E.pointY = F.pageY, E.startTime = D.timeStamp || Date.now(), E.options.onScrollStart && E.options.onScrollStart.call(E, D), E._bind(v), E._bind(w), E._bind(z))
    },
    _move: function _move(D) {
      var E = this,
        F = k ? D.touches[0] : D,
        G = F.pageX - E.pointX,
        H = F.pageY - E.pointY,
        I = E.x + G,
        J = E.y + H,
        K = D.timeStamp || Date.now();
      E.options.onBeforeScrollMove && E.options.onBeforeScrollMove.call(E, D), E.pointX = F.pageX, E.pointY = F.pageY, (0 < I || I < E.maxScrollX) && (I = E.options.bounce ? E.x + G / 2 : 0 <= I || 0 <= E.maxScrollX ? 0 : E.maxScrollX), (0 < J || J < E.maxScrollY) && (J = E.options.bounce ? E.y + H / 2 : 0 <= J || 0 <= E.maxScrollY ? 0 : E.maxScrollY), E.distX += G, E.distY += H, E.absDistX = a.abs(E.distX), E.absDistY = a.abs(E.distY);
      6 > E.absDistX && 6 > E.absDistY || (E.options.lockDirection && (E.absDistX > E.absDistY + 5 ? (J = E.y, H = 0) : E.absDistY > E.absDistX + 5 && (I = E.x, G = 0)), E.moved = !0, E._pos(I, J), E.dirX = 0 < G ? -1 : 0 > G ? 1 : 0, E.dirY = 0 < H ? -1 : 0 > H ? 1 : 0, 300 < K - E.startTime && (E.startTime = K, E.startX = E.x, E.startY = E.y), E.options.onScrollMove && E.options.onScrollMove.call(E, D))
    },
    _end: function _end(D) {
      if (!(k && 0 != D.touches.length)) {
        var G, H, N, E = this,
          F = k ? D.changedTouches[0] : D,
          I = {
            dist: 0,
            time: 0
          },
          J = {
            dist: 0,
            time: 0
          },
          K = (D.timeStamp || Date.now()) - E.startTime,
          L = E.x,
          M = E.y;
        if (E._unbind(v), E._unbind(w), E._unbind(z), E.options.onBeforeScrollEnd && E.options.onBeforeScrollEnd.call(E, D), !E.moved) {
          if (k) {
            for (G = F.target; 1 != G.nodeType;) G = G.parentNode;
            'SELECT' != G.tagName && 'INPUT' != G.tagName && 'TEXTAREA' != G.tagName && (H = document.createEvent('MouseEvents'), H.initMouseEvent('click', !0, !0, D.view, 1, F.screenX, F.screenY, F.clientX, F.clientY, D.ctrlKey, D.altKey, D.shiftKey, D.metaKey, 0, null), H._fake = !0, G.dispatchEvent(H))
          }
          return E._resetPos(200), void(E.options.onTouchEnd && E.options.onTouchEnd.call(E, D))
        }
        return 300 > K && E.options.momentum && (I = L ? E._momentum(L - E.startX, K, -E.x, E.scrollerW - E.wrapperW + E.x, E.options.bounce ? E.wrapperW : 0) : I, J = M ? E._momentum(M - E.startY, K, -E.y, 0 > E.maxScrollY ? E.scrollerH - E.wrapperH + E.y : 0, E.options.bounce ? E.wrapperH : 0) : J, L = E.x + I.dist, M = E.y + J.dist, (0 < E.x && 0 < L || E.x < E.maxScrollX && L < E.maxScrollX) && (I = {
          dist: 0,
          time: 0
        }), (0 < E.y && 0 < M || E.y < E.maxScrollY && M < E.maxScrollY) && (J = {
          dist: 0,
          time: 0
        })), I.dist || J.dist ? (N = a.max(a.max(I.time, J.time), 10), E.scrollTo(b(L), b(M), N), void(E.options.onTouchEnd && E.options.onTouchEnd.call(E, D))) : void(E._resetPos(200), E.options.onTouchEnd && E.options.onTouchEnd.call(E, D))
      }
    },
    _resetPos: function _resetPos(D) {
      var E = this,
        F = 0 <= E.x ? 0 : E.x < E.maxScrollX ? E.maxScrollX : E.x,
        G = 0 <= E.y || 0 < E.maxScrollY ? 0 : E.y < E.maxScrollY ? E.maxScrollY : E.y;
      return F == E.x && G == E.y ? void(E.moved && (E.options.onScrollEnd && E.options.onScrollEnd.call(E), E.moved = !1)) : void E.scrollTo(F, G, D || 0)
    },
    _mouseout: function _mouseout(D) {
      var E = D.relatedTarget;
      if (!E) return void this._end(D);
      for (; E = E.parentNode;)
        if (E == this.wrapper) return;
      this._end(D)
    },
    _transitionEnd: function _transitionEnd(D) {
      var E = this;
      D.target != E.scroller || (E._unbind('webkitTransitionEnd'), E._startAni())
    },
    _startAni: function _startAni() {
      var H, I, J, D = this,
        E = D.x,
        F = D.y,
        G = Date.now();
      if (!D.animating) return D.steps.length ? (H = D.steps.shift(), H.x == E && H.y == F && (H.time = 0), D.animating = !0, D.moved = !0, D.options.useTransition ? (D._transitionTime(H.time), D._pos(H.x, H.y), D.animating = !1, void(H.time ? D._bind('webkitTransitionEnd') : D._resetPos(0))) : void(J = function animate() {
        var L, M, K = Date.now();
        return K >= G + H.time ? (D._pos(H.x, H.y), D.animating = !1, D.options.onAnimationEnd && D.options.onAnimationEnd.call(D), void D._startAni()) : void(K = (K - G) / H.time - 1, I = a.sqrt(1 - K * K), L = (H.x - E) * I + E, M = (H.y - F) * I + F, D._pos(L, M), D.animating && (D.aniTime = p(J)))
      }, J())) : void D._resetPos(400)
    },
    _transitionTime: function _transitionTime(D) {
      this.scroller.style[c + 'TransitionDuration'] = D + 'ms'
    },
    _momentum: function _momentum(D, E, F, G, H) {
      var I = 6e-4,
        J = a.abs(D) / E,
        K = J * J / (2 * I),
        L = 0,
        M = 0;
      return 0 < D && K > F ? (M = H / (6 / (K / J * I)), F += M, J = J * F / K, K = F) : 0 > D && K > G && (M = H / (6 / (K / J * I)), G += M, J = J * G / K, K = G), K *= 0 > D ? -1 : 1, L = J / I, {
        dist: K,
        time: b(L)
      }
    },
    _offset: function _offset(D) {
      for (var E = -D.offsetLeft, F = -D.offsetTop; D = D.offsetParent;) E -= D.offsetLeft, F -= D.offsetTop;
      return {
        left: E,
        top: F
      }
    },
    _bind: function _bind(D, E, F) {
      (E || this.scroller).addEventListener(D, this, !!F)
    },
    _unbind: function _unbind(D, E, F) {
      (E || this.scroller).removeEventListener(D, this, !!F)
    },
    destroy: function destroy() {
      var D = this;
      D.scroller.style[c + 'Transform'] = '', D._unbind(s, window), D._unbind(u), D._unbind(v), D._unbind(w), D._unbind(z), D._unbind('mouseout', D.wrapper), D.options.useTransition && D._unbind('webkitTransitionEnd'), D.options.onDestroy && D.options.onDestroy.call(D)
    },
    refresh: function refresh() {
      var E, D = this;
      D.wrapperW = D.wrapper.clientWidth, D.wrapperH = D.wrapper.clientHeight, D.scrollerW = D.scroller.offsetWidth, D.scrollerH = D.scroller.offsetHeight, D.maxScrollX = D.wrapperW - D.scrollerW, D.maxScrollY = D.wrapperH - D.scrollerH, D.dirX = 0, D.dirY = 0, D.hScroll = D.options.hScroll && 0 > D.maxScrollX, D.vScroll = D.options.vScroll && (!D.options.bounceLock && !D.hScroll || D.scrollerH > D.wrapperH), E = D._offset(D.wrapper), D.wrapperOffsetLeft = -E.left, D.wrapperOffsetTop = -E.top, D.scroller.style[c + 'TransitionDuration'] = '0', D._resetPos(200)
    },
    scrollTo: function scrollTo(D, E, F, G) {
      var J, K, H = this,
        I = D;
      for (H.stop(), I.length || (I = [{
          x: D,
          y: E,
          time: F,
          relative: G
        }]), (J = 0, K = I.length); J < K; J++) I[J].relative && (I[J].x = H.x - I[J].x, I[J].y = H.y - I[J].y), H.steps.push({
        x: I[J].x,
        y: I[J].y,
        time: I[J].time || 0
      });
      H._startAni()
    },
    scrollToElement: function scrollToElement(D, E) {
      var G, F = this;
      D = D.nodeType ? D : F.scroller.querySelector(D);
      D && (G = F._offset(D), G.left += F.wrapperOffsetLeft, G.top += F.wrapperOffsetTop, G.left = 0 < G.left ? 0 : G.left < F.maxScrollX ? F.maxScrollX : G.left, G.top = 0 < G.top ? 0 : G.top < F.maxScrollY ? F.maxScrollY : G.top, E = E === void 0 ? a.max(2 * a.abs(G.left), 2 * a.abs(G.top)) : E, F.scrollTo(G.left, G.top, E))
    },
    disable: function disable() {
      this.stop(), this._resetPos(0), this.enabled = !1, this._unbind(v), this._unbind(w), this._unbind(z)
    },
    enable: function enable() {
      this.enabled = !0
    },
    stop: function stop() {
      q(this.aniTime), this.steps = [], this.moved = !1, this.animating = !1
    }
  }, 'undefined' == typeof exports ? window.iScroll = C : exports.iScroll = C
})();
